import {
  getCodeNameListAPI
} from "@/api/system/getCodeNameListAPI";
import {
  createStore
} from "vuex";
// 药房模块vuex
const pharmacyOptions = {
  namespaced: true,
  // 用dispatch('',value)指令触发,服务员
  actions: {
    // 获取常数项列表
    async getCodeNameList(context, value) {
      const {
        data: res
      } = await getCodeNameListAPI();
      if (res.statusCode == "C200") {
        context.state.codeNames = res.result;
      }
    },
  },
  // 用commit('',value)指令触发，厨师，与开发工具沟通
  mutations: {
    // 用户状态转换
    USERTYPEFORMAT(state, data) {
      console.log("用户转换函数触发了");
      var flag = false;
      for (let i = 0; i < state.codeNames.length; i++) {
        const item = state.codeNames[i];
        if (item.type == "userType" && item.key == data) {
          state.userType = item.value;
          flag = true;
          break;
        }
      }
      if (flag == false) state.userType = "量子纠缠用户";
    },
  },
  // 存储共享变量，菜
  state: {

    // 常数项列表
    codeNames: [
      {
        id:"000",
        type: "userType",
        key: 7004,
        value: "药房人员",
      },
      {
        id: '001',
        type: 'drugType',
        key: 1001,
        value: '西药'
      },
      {
        id: '002',
        type: 'drugType',
        key: 1002,
        value: '中药'
      },
      {
        id: '003',
        type: 'drugType',
        key: 1003,
        value: '仙药'
      },
      {
        id: '1',
        type: 'unit',
        key: 8001,
        value: '颗'
      },
      {
        id: '2',
        type: 'unit',
        key: 8002,
        value: '袋'
      },
      {
        id: '3',
        type: 'unit',
        key: 8003,
        value: '瓶'
      },
      {
        id: '4',
        type: 'unit',
        key: 8004,
        value: '根'
      },
      {
        id: '5',
        type: 'unit',
        key: 8005,
        value: '只'
      },

    ],

    // 用户类型
    userType: "",
  },
  // 计算属性
  getters: {},
};

// 医生模块
const doctorOptions = {
  namespaced: true,
  // 属性
  // 使用举例： this.$store.state.doctorOptions.doctorId
  state: {
    name: '123',
    age: 0,
    doctorId: '123456',
    patientHistoryId: '',
    examinationId: '',
    prescriptionId:''
  },

  //相当于get方法
  //使用举例：const name = $store.getters['getName']
  getters: {
    getName() {
      return state.name
    },
    getAge() {
      return state.age
    },
    getDoctorId() {
      return state.doctorId
    },
    getPatientHistoryId() {
      return state.patientHistoryId
    },
    getExaminationId() {
      return state.examinationId
    }
  },

  // 相当于set方法，可在actions中提交内容

  mutations: {
    setName(state, val) {
      state.name = val
    },
    setAge(state, val) {
      state.age = val
    },
    setPatientHistoryId(state, data) {
      state.patientHistoryId = data;
    },
    setExaminationId(state, data) {
      state.examinationId = data;
    }
  },

  // 相当于事件
  //使用举例：$store.dispatch('setNameAc','456')
  actions: {
    setNameAc(context, val) {
      context.commit('setName', val)
    },
    setAgeAc(context, val) {
      context.commit('setAge', val)
    },
    setPatientHistoryIdAc(context, val) {
      context.commit('setPatientHistoryId', val)
    },

  },
}

export default createStore({
  modules: {
    pharmacyOptions,
    doctorOptions,
  },
});
